import 'package:app/pages/personnel_module/personnel_model.dart';
import 'package:app/services/address.dart';
import 'package:app/services/dio_manager.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

class PersonnelController extends GetxController {



  /// 是否多选
  bool isMultiple = false;

  PersonnelModel? model;
  DepartmentModel? departmentModel;


  TextEditingController editingController = TextEditingController();

  List<String> personnelNameArr = [];
  List<int> personnelIdArr = [];

  /// 获取员工
  requestDataWithHrEmployee(var search) async {
    var params = {'limit': '1000','search':search};

    var json = await DioManager()
        .kkRequest(Address.hrEmployee, params: params, method: 'get',);

    PersonnelModel _model = PersonnelModel.fromJson(json);

    model = _model;

    initData();


    update();
  }

  /// 获取部门树
  requestDataWithHrDepartment() async {
    var json =
        await DioManager().kkRequest(Address.hrDepartment, method: 'get');

    DepartmentModel model = DepartmentModel.fromJson(json);
    departmentModel = model;
    update();
  }
  /// 点击选中部门人数
  tapClickCheck(var index){
    /// 单选情况
    if(isMultiple == false){

      for(int i = 0;i<model!.data!.length;i++){
        model?.data?[i].isSelection = false;
      }
      model?.data?[index].isSelection = true;
      personnelNameArr.clear();
      personnelIdArr.clear();
      personnelNameArr.add(model!.data![index].name!);
      personnelIdArr.add(model!.data![index].userId!);
      update();
      return;
    }
    /// 多选情况
    if(model?.data?[index].isSelection==false){
      model?.data?[index].isSelection = true;
      // var params = {
      //   'name': model?.data?[index].name,
      //   'userId': model?.data?[index].userId,
      // };
      personnelNameArr.add(model!.data![index].name!);
      personnelIdArr.add(model!.data![index].userId!);
      // print('personnelArr === ${personnelNameArr}');

    }else{
      model?.data?[index].isSelection = false;
      personnelNameArr.remove(model?.data?[index].name);
      personnelIdArr.remove(model?.data?[index].userId);
      // print('personnelArr === ${personnelArr}');
    }

    update();
  }
  /// 点击确定返回
  tapClickEnter(){

    if(personnelIdArr.length == 0){
      BotToast.showText(text: '请选择人员');
      return;
    }


    Get.back(result: {'personnelNameArr':personnelNameArr,'personnelIdArr':personnelIdArr});

    // Get.back(result: personnelArr);
  }


  initData()async{

    if(Get.arguments['isMultiple'] == true){
      // personnelIdArr = Get.arguments['personnelIdArr'].split(',');

      for(int i = 0;i<model!.data!.length;i++){

        for(int j = 0;j<personnelIdArr.length;j++){
          print('kkkkkkkkkk  ${personnelIdArr[j]} == ${model!.data![i].id}');
          print('kkkkkkkkkk  ${model!.data![i].id == personnelIdArr[j]}');
          if(model!.data![i].id == personnelIdArr[j]){
            model?.data?[i].isSelection = true;
            // personnelNameArr.add(model!.data![i].name!);
            // personnelIdArr.add(model!.data![i].userId!);
          }
        }

        // if(model!.data![i].id == Get.arguments['personnelIdArr']){
        //   print('ssss ${model!.data![i].id == Get.arguments['personnelIdArr']}');
        //
        //   model?.data?[i].isSelection = true;
        // }
      }
    }else if(Get.arguments['personnelIdArr']!=null){
      for(int i = 0;i<model!.data!.length;i++){
        print('ssssss ${Get.arguments['personnelIdArr'][0]} == ${model!.data![i].id}');
        if(model!.data![i].id == Get.arguments['personnelIdArr'][0]){
          // print('ssss ${model!.data![i].id == int.parse(Get.arguments['personnelIdArr'][0])}');
          model?.data?[i].isSelection = true;

          // personnelNameArr.add(model!.data![i].name!);
          // personnelIdArr.add(model!.data![i].userId!);
        }
      }
    }



  }


  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();

    isMultiple  = Get.arguments['isMultiple'];

    print('arguments ==== ${Get.arguments}');
    Get.arguments['personnelIdArr']==null?'': personnelIdArr = Get.arguments['personnelIdArr'];
    Get.arguments['personnelNameArr']==null?'': personnelNameArr = Get.arguments['personnelNameArr'];

    requestDataWithHrEmployee('');
    requestDataWithHrDepartment();



  }
}
